//
//  MYGTriageViewController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/29.
//

import UIKit
import QMUIKit

class MYGTriageViewController: MYGBaseController {
    var authBlock:((_ index:Int,_ type:MYGIMMessageType)->Void)?
    var item : MYGSysOtherMessageModel?
    var model:MYGSysMessageModel? {
        didSet {
            guard let item = model else {
                return
            }
            reView.nameLabel.text = "申请人"
            reView.contentLabel.text = item.from_doctor.name
            reView.timeLabenl.text = item.created_at
            
            shareView.nameLabel.text = "确认人"
            let name = MYGUserManager.getUser()?.doctor.name ?? ""
            let hospital = MYGUserManager.getUser()?.doctor.hospital ?? ""
            let department = MYGUserManager.getUser()?.doctor.department ?? ""
            shareView.contentLabel.text = "\(name) \(hospital) \(department)"
            shareView.timeLabenl.text = "待同意 \(item.updated_at)"

            contentLabel.text = item.message
        }
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        getSysMessageList()
        configUI()
    }
    
    func configUI(){
        title = "分诊待办"
        view.backgroundColor = .rgba(255, 255, 255, 1)
        
        view.addSubview(reView)
        reView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(23)
            make.top.equalTo(17)
            make.height.equalTo(92)
        }
    
        view.addSubview(shareView)
        shareView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(23)
            make.top.equalTo(reView.snp.bottom)
            make.height.equalTo(92)
        }
//        view.addSubview(accpetView)
//        accpetView.snp.makeConstraints { make in
//            make.left.right.equalToSuperview().inset(23)
//            make.top.equalTo(shareView.snp.bottom)
//            make.height.equalTo(92)
//        }
        
        view.addSubview(contentLabel)
        //view.addSubview(delegateButton)
        view.addSubview(reButton)
        view.addSubview(agreenButton)
        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(shareView.snp.bottom).offset(20)
            //make.height.equalTo(20)
            make.left.equalTo(shareView.snp.left).offset(74)
            make.right.equalTo(shareView)
        }
//        delegateButton.snp.makeConstraints { make in
//            make.left.equalTo(contentLabel)
//            make.top.equalTo(contentLabel.snp.bottom).offset(15)
//            make.height.equalTo(20)
//        }
        
        agreenButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(30)
            make.top.equalTo(contentLabel.snp.bottom).offset(17.5)
            make.width.equalTo(64)
            make.height.equalTo(21)
        }
        reButton.snp.makeConstraints { make in
            make.right.equalTo(agreenButton.snp.left).offset(-16.5)
            make.top.equalTo(contentLabel.snp.bottom).offset(17.5)
            make.width.equalTo(64)
            make.height.equalTo(21)
        }
        
    }

    // MARK: - lazy load
    
    private lazy var reView:MYGShareReportNotiCell = {
        let view = MYGShareReportNotiCell()
        view.backgroundColor = .white
        return view
    }()
    private lazy var shareView:MYGShareReportNotiCell = {
        let view = MYGShareReportNotiCell()
        view.backgroundColor = .white
        return view
    }()
//    private lazy var accpetView:MYGShareReportNotiCell = {
//        let view = MYGShareReportNotiCell()
//        return view
//    }()
    
    private lazy var contentLabel:QMUILabel = {
        let lab = QMUILabel()
        lab.textColor = .rgba(44, 44, 44, 1)
        lab.font = .medium(14)
        lab.text = "陈算患者申请绑定您为主治医师请您确认"
        lab.numberOfLines = 0
        lab.backgroundColor = .white
        lab.layer.cornerRadius = 10
        lab.layer.masksToBounds = true
        lab.layer.maskedCorners = [.layerMaxXMinYCorner,.layerMinXMaxYCorner,.layerMaxXMaxYCorner]
        lab.contentEdgeInsets = UIEdgeInsets(inset: 5)
        return lab
    }()
    
//    private lazy var delegateButton: QMUIButton = {
//        let btn = QMUIButton()
//        btn.setImage(UIImage(named: "Register/delegate"), for: .normal)
//        btn.setImage(UIImage(named: "Register/delegate_sel"), for: .selected)
//        btn.spacingBetweenImageAndTitle = 4
//        btn.imagePosition = .left
//        btn.setAttributedTitle(NSMutableAttributedString.rz.colorfulConfer(confer: { confer in
//            confer.text("《预览病例》")?.font(.regular(14)).textColor(.rgba(69, 131, 250, 1)).tapActionByLable("delegate")
//        }), for: .normal)
//        btn.titleLabel?.isUserInteractionEnabled = true
//        btn.addTarget(self, action: #selector(clickdelegateButton(_:)), for: .touchUpInside)
//        return btn
//    }()
    
    private lazy var agreenButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("同意", for: .normal)
        btn.setTitleColor(.rgba(255, 255, 255, 1), for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.addTarget(self, action: #selector(clickAgreeButton), for: .touchUpInside)
        btn.setBackgroundColor(color: .rgba(77, 120, 237, 1), forState: .normal)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 10
        return btn
    }()
    private lazy var reButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("拒绝", for: .normal)
        btn.setTitleColor(.rgba(151, 151, 151, 1), for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.addTarget(self, action: #selector(clickRefuseButton), for: .touchUpInside)
        
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 10
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.rgba(151, 151, 151, 1).cgColor
        return btn
    }()
    
    @objc func clickdelegateButton(_ sender:QMUIButton){
        sender.isSelected = !sender.isSelected
        
    }
    
    func patientApplication(status:String,completion:@escaping ((Bool)->Void)){
        MYGLoading.show()
        var params:[String:Any] = [:]
        params["patient_id"] = item?.patient_id
        params["msg_status"] = status
        params["from_doctor_id"] = model?.from_doctor.id
        params["case_range"] = item?.case_range
        params["case_id"] = item?.case_id

        MMYGNetwork<MYGMessageApi,MYGEmptyModel>.request(target: .doctorCofirm(params:params )) { _ in
            MYGLoading.hide()
            completion(true)
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
            completion(false)
        }

    }
    
    // 拒绝
    @objc func clickRefuseButton(){
        patientApplication(status: "2") { [self] isSuc in
            if !isSuc {
                return
            }
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    //同意分享
    @objc func clickAgreeButton(){
        patientApplication(status: "1") { [self] isSuc in
            if !isSuc {
                return
            }
           self.navigationController?.popViewController(animated: true)
        }
    }
    
    private func getSysMessageList() {
      
        MYGLoading.show()
        let patient_id = model?.from_patient_id ?? 0
        MMYGNetwork<MYGMessageApi, MYGMessageMessageListModel>.request(target: .getSysMessageList(patient_id: "\(patient_id)")) { [weak self] item in
            MYGLoading.hide()
            guard let items = item?.data else {
                return
            }
            
            let model:MYGSysOtherMessageModel = items.first!
            self?.item = model
            
            
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    

}
